package com.thyc.server;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class StartComponent {

    private static final String SOURCE_DATA = "SOURCE_DATA";

    @Resource
    private ParseData parseData;

    @Resource
    RedisTemplate redisTemplate;

    @Async
    public void run() {
        for (int i = 0; i < 17; i++) {
            new Thread(() -> toDo()).start();
        }
    }

    void toDo() {
        for (; ; ) {
            List<Integer> dataList = (List<Integer>) redisTemplate.opsForList().rightPop(SOURCE_DATA);
            if (ObjectUtil.isNotEmpty(dataList)) {
                parseData.parseData(dataList);
            }
            ThreadUtil.safeSleep(300);
        }
    }
}
